package com.bobi.tfca.controller.message;

import com.bobi.tfca.model.entity.MemberEntity;
import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.model.vo.MessageVO;
import com.bobi.tfca.service.MessageService;
import com.bobi.tfca.web.annotation.CurrentUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李文发
 * @date 2024/4/10
 */
@RestController
@RequestMapping("/letter")
@Api(tags = "信件接口")
@Slf4j
public class LetterController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/findAllLetters")
    @ApiOperation(value = "查询全部信件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "用户信息")
    })
    public R<List<MessageVO>> findAllLetters(@CurrentUser MemberEntity member) {
        Integer memberId = member.getMemberId();
        List<MessageVO> list = messageService.findAllLetters(memberId);
        return R.data(list);
    }

    @GetMapping("/getLetterById/{letterId}")
    @ApiOperation(value = "查询信件详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "用户信息"),
            @ApiImplicitParam(name = "letterId", value = "信件id")
    })
    public R<MessageVO> getLetterById(@CurrentUser MemberEntity member, @PathVariable("letterId") Long letterId) {
        MessageVO messageVO = messageService.getLetterById(member, letterId);
        return R.data(messageVO);
    }

    @PostMapping("/writeLetter/{receiverId}")
    @ApiOperation(value = "写信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "用户信息"),
            @ApiImplicitParam(name = "receiverId", value = "收信人"),
            @ApiImplicitParam(name = "letterContent", value = "内容")
    })
    public R<String> writeLetter(@CurrentUser MemberEntity member, @PathVariable("receiverId") Integer receiverId, @RequestBody String letterContent) {
        Integer memberId = member.getMemberId();
        Boolean success = messageService.writeLetter(memberId, receiverId, letterContent);
        return success ? R.success("已发送") : R.failed("发送失败");
    }

    @ApiOperation(value = "查询未读信件数量")
    @GetMapping("/getUnReadLetterNum")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "member", value = "用户信息")})
    public R<Integer> getUnReadLetterNum(@CurrentUser MemberEntity member) {
        Integer memberId = member.getMemberId();
        Integer unReadLetterNum = messageService.getUnReadLetterNum(memberId);
        return R.data(unReadLetterNum);
    }
}
